package server;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;

public class TCPDesktopServer implements Runnable
{
    public static final int SERVERPORT = 5556;
    
    public void run()
    {
        String[] array = new String[21];
        try
        {
            System.out.println("Server: Connecting...");
            ServerSocket serverSocket = new ServerSocket(SERVERPORT);
            while (true)
            {
                System.out.println("Server:Connected.");
                Socket client = serverSocket.accept();
                System.out.println("Server: Receiving...");
                try
                {
                    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    String str = null;
                    while ((str = in.readLine()) != null)
                    {
                        System.out.println("接收到的原始字符串：" + str);
                        array = str.split("&");
                        String[] arrayData = new String[21];
                        for (String data : array)
                        {
                            String[] ds = data.split("=");
                            String val = "";
                            ds[0] = ds[0].toUpperCase();
                            if (ds[0].startsWith("PLC"))
                            {
                                if (ds.length == 2)
                                    val = Integer.decode("0X" + ds[1]).toString();
                            }
                            else
                            {
                                if (ds.length == 2)
                                    val = ds[1];
                            }
                            if ("PLC4001".equals(ds[0]) || "PH".equals(ds[0]))
                            {
                                arrayData[0] = val;
                                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(System.currentTimeMillis())
                                    + "  ph:" + arrayData[0]);
                            }
                            else if ("PLC4002".equals(ds[0]) || "ORP".equals(ds[0]))
                            {
                                arrayData[1] = val;
                                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(System.currentTimeMillis())
                                    + "  orp:" + arrayData[1]);
                            }
                            else if ("PLC4003".equals(ds[0]) || "CLO".equals(ds[0]))
                            {
                                arrayData[3] = val;
                                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(System.currentTimeMillis())
                                    + "  CLO:" + arrayData[3]);
                            }
                            else if ("PLC4004".equals(ds[0]) || "TEM".equals(ds[0]))
                            {
                                arrayData[2] = val;
                                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(System.currentTimeMillis())
                                    + "  TEM:" + arrayData[2]);
                            }
                            else if ("PLC4005".equals(ds[0]) || "OCLO".equals(ds[0]))
                            {
                                arrayData[4] = val;
                            }
                            else if ("PLC4006".equals(ds[0]) || "CCLO".equals(ds[0]))
                            {
                                arrayData[5] = val;
                            }
                            else if ("PLC4007".equals(ds[0]) || "ESIL".equals(ds[0]))
                            {
                                arrayData[7] = val;
                            }
                            else if ("PLC4008".equals(ds[0]) || "EC1".equals(ds[0]))
                            {
                                arrayData[8] = val;
                            }
                            else if ("PLC4009".equals(ds[0]) || "EC2".equals(ds[0]))
                            {
                                arrayData[9] = val;
                            }
                            else if ("PLC4010".equals(ds[0]) || "EC3".equals(ds[0]))
                            {
                                arrayData[10] = val;
                            }
                            else if ("PLC4011".equals(ds[0]) || "EC4".equals(ds[0]))
                            {
                                arrayData[11] = val;
                            }
                            else if ("PLC4012".equals(ds[0]) || "EC5".equals(ds[0]))
                            {
                                arrayData[12] = val;
                            }
                            else if ("PLC4013".equals(ds[0]) || "EC6".equals(ds[0]))
                            {
                                arrayData[13] = val;
                            }
                            else if ("PLC4014".equals(ds[0]) || "EC7".equals(ds[0]))
                            {
                                arrayData[14] = val;
                            }
                            else if ("PLC4015".equals(ds[0]) || "EC8".equals(ds[0]))
                            {
                                arrayData[15] = val;
                            }
                            else if ("PLC4016".equals(ds[0]) || "EC9".equals(ds[0]))
                            {
                                arrayData[16] = val;
                            }
                            else if ("PLC4017".equals(ds[0]) || "EC10".equals(ds[0]))
                            {
                                arrayData[17] = val;
                            }
                            else if ("PLC4018".equals(ds[0]) || "EC11".equals(ds[0]))
                            {
                                arrayData[18] = val;
                            }
                            else if ("PLC4019".equals(ds[0]) || "EC12".equals(ds[0]))
                            {
                                arrayData[19] = val;
                            }
                            else if ("PLC4020".equals(ds[0]) || "EC13".equals(ds[0]))
                            {
                                arrayData[20] = val;
                            }
                            else if ("MAC".equals(ds[0]) || "$MAC".equals(ds[0]))
                            {
                                arrayData[6] = val;
                            }
                            
                        }
                        System.out.println("分析后的字符串：" + arrayData);
                        GetRadomValue.getInstance().pushArrayValue(arrayData);
                    }
                    
                }
                catch (Exception e)
                {
                    System.out.println("Server: Error");
                    e.printStackTrace();
                }
                finally
                {
                    client.close();
                    System.out.println("Server: Done.");
                }
                
            }
            
        }
        catch (Exception e)
        {
            System.out.println("Server: Error");
            e.printStackTrace();
        }
    }
    
}